package com.jm.timing;

import com.jm.model.system.Pl;
import com.jm.service.i.system.PlService;
import com.jm.tools.RandomTools;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@Slf4j
public class TimingData {

    private final PlService plService;

    @Autowired
    public TimingData(PlService plService) {
        this.plService = plService;
    }

    @Scheduled(initialDelay = 5000, fixedRate = 5000)
    @Async
    public void handler5() {
        this.handler(5);
    }

    private void handler(int second) {
        log.debug("间隔{}", second);
        List<Pl> pls = this.plService.list();
        pls = pls.parallelStream().peek(p -> {
            long score = RandomTools.randomLong(Math.toIntExact(p.getMinScopeScore()), Math.toIntExact(p.getMaxScopeScore()));
            p.setVtScore(score);
        }).toList();
        this.plService.updateBatchById(pls);
    }
}
